//
//  UIView+Frame.swift
//  Scanread
//
//  Created by Macintosh on 2023/6/23.
//

import UIKit

extension UIView {
    /// view的x
    var x: CGFloat{
        get{ return frame.origin.x }
        set{ self.frame.origin.x = newValue }
    }
    
    var left: CGFloat{
        get{ return frame.origin.x }
        set{ self.frame.origin.x = newValue }
    }

    /// view的y
    var y: CGFloat{
        get{ return frame.origin.y }
        set{ self.frame.origin.y = newValue }
    }
    
    var top: CGFloat{
        get{ return frame.origin.y }
        set{ self.frame.origin.y = newValue }
    }

    /// view的宽度
    var width: CGFloat {
        get{ return self.frame.size.width }
        set{
            var frameCopy = frame
            frameCopy.size.width = newValue
            frame = frameCopy
        }
    }

    /// view的高度
    var height: CGFloat {
        get{ return self.frame.size.height }
        set{ var frameCopy = frame
            frameCopy.size.height = newValue
            frame = frameCopy
        }
    }

    /// view中心X值
    var centerX: CGFloat{
        get{ return self.center.x }
        set{ self.center = CGPoint(x: newValue, y: self.center.y) }
    }

    /// view中心Y值
    var centerY: CGFloat{
        get{ return self.center.y }
        set{ self.center = CGPoint(x: self.center.y, y: newValue) }
    }

    /// view的size
    var size: CGSize{
        get{ return self.frame.size }
        set{
            var frameCopy = frame
            frameCopy.size = newValue
            frame = frameCopy
        }
    }

    /// view的origin
    var origin: CGPoint {
        get{ return self.frame.origin }
        set{
            var frameCopy = frame
            frameCopy.origin = newValue
            frame = frameCopy
        }
    }

    var viewCenter: CGPoint{
        get{return CGPoint(x: width * 0.5, y: height * 0.5)}
    }
    
    var maxX: CGFloat{
        get{return self.x + self.width}
        set{x = newValue - self.width}
    }
    
    var maxY: CGFloat{
        get{return self.y + self.height}
        set{y = newValue - self.height}
    }
    
    var right: CGFloat {
        get{return self.x + self.width}
        set{x = newValue - self.width}
    }

    var bottom: CGFloat {
        get{return self.y + self.height}
        set{y = newValue - self.height}
    }
    
    /// 圆角
    @IBInspectable var cornerRadius: CGFloat {
        get {return layer.cornerRadius}
        set {
            self.layer.cornerRadius = newValue
            self.layer.masksToBounds = true
        }
    }

}

extension UIView {
    enum CornerType: Int {
        case top // 上
        case letf // 左
        case topLeft // 上左
        case right // 右
        case bottom // 底
        case all // 所有
    }
    
    /// 部分圆角 @available(iOS 11.0, *)
    /// - Parameters:
    ///   - type: 圆角类型
    ///   - radius: 圆角半径
    @available(iOS 11.0, *)
    func cornerRadius(type: CornerType, radius: CGFloat) {
        var corenrs: CACornerMask
        self.layoutIfNeeded()
        
        switch type {
        case .top:
            corenrs = CACornerMask(rawValue: CACornerMask.layerMinXMinYCorner.rawValue | CACornerMask.layerMaxXMinYCorner.rawValue)
        case .letf:
            corenrs = CACornerMask(rawValue: CACornerMask.layerMinXMinYCorner.rawValue | CACornerMask.layerMinXMaxYCorner.rawValue)
        case .topLeft:
            corenrs = .layerMinXMinYCorner
        case .right:
            corenrs = CACornerMask(rawValue: CACornerMask.layerMaxXMinYCorner.rawValue | CACornerMask.layerMaxXMaxYCorner.rawValue)
        case .bottom:
            corenrs = CACornerMask(rawValue: CACornerMask.layerMinXMaxYCorner.rawValue | CACornerMask.layerMaxXMaxYCorner.rawValue)
        case .all:
            corenrs = CACornerMask(rawValue: CACornerMask.layerMinXMinYCorner.rawValue | CACornerMask.layerMinXMaxYCorner.rawValue | CACornerMask.layerMaxXMinYCorner.rawValue | CACornerMask.layerMaxXMaxYCorner.rawValue)
        }
        
        self.layer.cornerRadius = radius
        self.layer.maskedCorners = corenrs
    }
}

extension UIView {
    // 将当前视图转为UIImage
    func screenshotImage() -> UIImage {
        let renderer = UIGraphicsImageRenderer(bounds: bounds)
        return renderer.image { rendererContext in
            layer.render(in: rendererContext.cgContext)
        }
    }
    
    func snapshot(of rect: CGRect) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        defer { UIGraphicsEndImageContext() }
        
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        
        context.translateBy(x: -rect.origin.x, y: -rect.origin.y)
        layer.render(in: context)
        
        return UIGraphicsGetImageFromCurrentImageContext()
    }

}

extension Date {

    // 获取星期几
    func weekDay() -> String {
        let weekDays = [NSNull.init(),"周日","周一","周二","周三","周四","周五","周六"] as [Any]
        let calendar = NSCalendar.init(calendarIdentifier: .gregorian)
        let timeZone = NSTimeZone.init(name: "Asia/Shanghai")
        calendar?.timeZone = timeZone! as TimeZone
        let calendarUnit = NSCalendar.Unit.weekday
        let theComponents = calendar?.components(calendarUnit, from: self)

        return weekDays[(theComponents?.weekday)!] as! String
    }
    
}

extension UIView {

    @discardableResult func addGradientLayer(_ colors: [UIColor], startPoint: CGPoint = CGPoint(x: 0, y: 0.5), endPoint: CGPoint = CGPoint(x: 1, y: 0.5)) -> CAGradientLayer {
      if let gradientLayer = gradientLayer { return gradientLayer }

      let gradient = CAGradientLayer()

      gradient.frame = bounds
      gradient.colors = colors.map { $0.cgColor }
      gradient.startPoint = startPoint
      gradient.endPoint = endPoint
      layer.insertSublayer(gradient, at: 0)

      return gradient
  }

  func removeGradientLayer() -> CAGradientLayer? {
      gradientLayer?.removeFromSuperlayer()

      return gradientLayer
  }

  func resizeGradientLayer() {
      gradientLayer?.frame = bounds
  }

  fileprivate var gradientLayer: CAGradientLayer? {
      return layer.sublayers?.first as? CAGradientLayer
  }
    
}
